Page({
    data: {
        address: '点击选择，请勾选哦~',
        type: 'buy',
        message: '',
        contact: '',
        _latitude: '',
        _longitude: ''
    },

    openLocation() {
        wx.chooseLocation({
            success: (res) => {
                const { address, latitude, longitude } = res
                this.setData({
                    address
                })
                this.data._latitude = latitude
                this.data._longitude = longitude
            }
        })
    },

    handleTypeChange(e) {
        this.setData({
            type: e.detail.value
        })
    },

    handleSubmit() {
        const { address, contact, message, type, _latitude, _longitude } = this.data
        const data = {
            id: Math.floor(Math.random() * 100000000),
            address,
            contact,
            message,
            type,
            latitude: _latitude,
            longitude: _longitude
        }
        // 第三方应用
        // wx.request({
        //   url: 'http://www.abc.com',
        //   method: 'POST',
        //   data,
        //   success(res) {
        //       console.log(res)
        //   },
        //   fail(res) {
        //     console.log(res)
        //   }
        // })

        // 使用云平台
        const db = wx.cloud.database()
        db.collection('minipet').add({
            data
        })
        .then(res => {
            wx.navigateBack()
        })
    },

    handleMessageInput(e) {
        this.setData({
            message: e.detail.value
        })
    },

    handleContactInput(e) {
        this.setData({
            contact: e.detail.value
        })
    }
})